package com.example.smartall.fragment.Hospital;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.smartall.R;
import com.example.smartall.adapter.HospitalRegistrationAdapter;
import com.example.smartall.entity.HospitalCategoryBean;
import com.example.smartall.fragment.base.BaseFragment;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *普通诊
 */
public class CommonFragment extends BaseFragment implements Serializable{

    private HospitalCategoryBean.RowsBean categoryBeans;
    private String patientName;
    private List<DataBean> dataBeanList = new ArrayList<>();
    private ListView lvRegister;
    private HospitalRegistrationAdapter adapter;

    public CommonFragment() {

    }

    public CommonFragment(HospitalCategoryBean.RowsBean categoryBeans,String patientName){
        this.categoryBeans = categoryBeans;
        this.patientName = patientName;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("CommonFragment","CommonFragment创建");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_common, container, false);
        init(view);
        initEvent();
        createData();
        initData();
        return view;
    }

    private void initData() {
        adapter = new HospitalRegistrationAdapter(getActivity(),dataBeanList,categoryBeans,patientName);
        lvRegister.setAdapter(adapter);
    }

    private void init(View view) {
        lvRegister = (ListView) view.findViewById(R.id.lv_Register);
    }

    private void initEvent() {

    }

    /**
     * 原本是想获得当前时间 在当前时间上 生成12条都相隔30分钟的date数据填充列表，没给api，结果自己不会写
     */
    private void createData(){
        Date date = new Date();
        String[] res = new SimpleDateFormat("YYYY-MM-dd HH:mm").format(date).split(" ");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        Log.e("com/example/smartall/fragment/Hospital/CommonFragment.java/createData","abbas等你来修Bug");

        for (int i = 0 ; i < 12 ; i++) {
            calendar.add(Calendar.MINUTE,i*30);
            DataBean dataBean = new DataBean();
            dataBean.date = res[0];
            dataBean.time = res[1];
            dataBean.type = categoryBeans.getCategoryName();
            dataBean.when = new SimpleDateFormat("EEEE").format(date) + week(calendar.getTime());
            dataBeanList.add(dataBean);
            Log.e("tag",dataBean.toString());
        }
    }

    /**
     * 判断上午和下午
     * @param target 需要判断的日期
     * @return 上午、下午
     */
    private String week(Date target){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(target);
        if (Calendar.AM == calendar.get(Calendar.AM_PM)) {
            return "上午";
        }else {
            return "下午";
        }
    }

    public static class DataBean implements Serializable {
        String type,time,date,when;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public String getWhen() {
            return when;
        }

        public void setWhen(String when) {
            this.when = when;
        }
    }
}